package testing.src.akash.java.code;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;
  
public class LargeMultiply
{
    public static void main(String[] args) throws IOException
    {   
        LargeMultiply largeMultiply = new LargeMultiply();
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter first number :");
        String number1 = reader.readLine();
        System.out.println("Enter Second number :");
        String number2 = reader.readLine();
        StringBuffer num1 = new StringBuffer(number1);
        StringBuffer num2 = new StringBuffer(number2);
        System.out.println("the product of these two numbers is :" + largeMultiply.multiply(num1, num2));

    }

    public StringBuffer multiply(StringBuffer num1, StringBuffer num2)
    {
        
    	StringBuffer number1 = new StringBuffer(num1.toString());
    	StringBuffer number2 = new StringBuffer(num2.toString());
        number1.reverse();
        number2.reverse();
        List<StringBuffer> stack = new LinkedList<StringBuffer>();
        stack = getAddableStack(number1, number2);
        StringBuffer result = new StringBuffer();
        LargeSum largeSum = new LargeSum();
        for(StringBuffer stringBuffer : stack)
        {
            result = largeSum.addNumbers(result, stringBuffer);
        }
        return result;
    }

    private static List<StringBuffer> getAddableStack(StringBuffer num1, StringBuffer num2)
    {
    	  StringBuffer smallerNumber = new StringBuffer();
          StringBuffer largerNumber = new StringBuffer();
      if(num1.length()!=num2.length()){
    	   smallerNumber = num1.length() < num2.length() ? num1 : num2;
           largerNumber = num1.length() > num2.length() ? num1 : num2; 
      }else {
    	  smallerNumber = num1;
    	  largerNumber = num2;
      }
      

        List<StringBuffer> stack = new LinkedList<StringBuffer>();
        StringBuffer stepMultiplicationString = new StringBuffer();
        for(int i = 0; i < smallerNumber.length(); i++)
        {
            stepMultiplicationString = multiplyBySingleNumber(Integer.parseInt(smallerNumber.substring(i, i + 1)),
                            largerNumber);
            for(int j = 0; j < i; j++)
            {
                stepMultiplicationString.append(0);
            }
            stack.add(stepMultiplicationString);
        }

        return stack;
    }

    private static StringBuffer multiplyBySingleNumber(int multiplier, StringBuffer bigNumber)
    {
        StringBuffer result = new StringBuffer();
        int length = bigNumber.length();

        int digit = 0;
        int carry = 0;
        int product = 0;
        NumCarry numCarry = new NumCarry();
        for(int i = 0; i < length; i++)
        {
            digit = Integer.parseInt(bigNumber.substring(i, i + 1));
            product = digit * multiplier + carry;
            numCarry.reSetNumCarry(product);
            carry = numCarry.getCarry();
            result.append(numCarry.getNum());
        }
        if(carry>0){
            result.append(carry);
        }
        return result.reverse();
    }

}
